cmake_minimum_required(VERSION 3.26)

# 语言环境配置 ###########################################################################################################
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_BUILD_WITH_INSTALL_RPATH True)

# 项目配置 ##############################################################################################################
project(cppguide_test VERSION 1.0.0 LANGUAGES CXX)

# vcpkg ################################################################################################################

# fmt 库
find_package(fmt CONFIG REQUIRED)
get_target_property(fmt_LOCATION fmt::fmt LOCATION)
get_filename_component(fmt_DIRECTORY ${fmt_LOCATION} DIRECTORY)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
    string(REPLACE "debug/" "" fmt_DIRECTORY ${fmt_DIRECTORY})
endif()

message(STATUS "fmt_DIRECTORY:${fmt_DIRECTORY}")

# spdlog 库
find_package(spdlog CONFIG REQUIRED)
get_target_property(spdlog_LOCATION spdlog::spdlog LOCATION)
get_filename_component(spdlog_DIRECTORY ${spdlog_LOCATION} DIRECTORY)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
    string(REPLACE "debug/" "" spdlog_DIRECTORY ${spdlog_DIRECTORY})
endif()

# 不需要 debug 日志时,注释下面两行
add_compile_options(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG)
add_compile_options(-DCPP_GUIDE_LOG_LEVEL=1)

message(STATUS "spdlog_DIRECTORY:${spdlog_DIRECTORY}")

# pugixml
find_package(pugixml CONFIG REQUIRED)
get_target_property(pugixml_LOCATION pugixml::shared LOCATION)
get_filename_component(pugixml_DIRECTORY ${pugixml_LOCATION} DIRECTORY)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
    string(REPLACE "debug/" "" pugixml_DIRECTORY ${pugixml_DIRECTORY})
endif()

message(STATUS "pugixml_LOCATION:${pugixml_LOCATION}")

# 编译相关配置 ###########################################################################################################

# 生成 compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

aux_source_directory(. test_srcs)
add_executable(${PROJECT_NAME} ${test_srcs})

target_include_directories(${PROJECT_NAME} PRIVATE
    ${PROJECT_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/include
    ${CMAKE_SOURCE_DIR}/third
)

target_link_libraries(${PROJECT_NAME} PRIVATE
    ${util_lib_name}
    fmt::fmt
    spdlog::spdlog
    pugixml::shared
)

set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${dist_dir}/test)

set_target_properties(${PROJECT_NAME} PROPERTIES INSTALL_RPATH "\${ORIGIN}/../lib")

# 构建后动作
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD

    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/res/students.xml ${dist_dir}/test/students.xml
)

# 测试相关配置 ###########################################################################################################
# 执行所有测试
# ctest --test-dir build
add_test(NAME test_all COMMAND ${PROJECT_NAME})
set_tests_properties(test_all PROPERTIES WORKING_DIRECTORY ${dist_dir}/test)

# test add_custom_command
# ctest --test-dir build -R test_common
add_test(NAME test_common COMMAND ${PROJECT_NAME} -ts=test_common)